package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.wallet.dao.AccountLogDao;
import cn.wp.ewallet.wallet.model.entity.AccountLogDo;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName AccountLogDaoImpl
 * @Author ruixule
 * @Description TODO
 * @Date 2020/1/7 16:55
 */
@Repository
public class AccountLogDaoImpl  implements AccountLogDao {


    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    /**
     * 账户变动的保存
     * @param accountLog
     * @return
     */
    @Override
    public int insertAccountLog(AccountLogDo accountLog) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("insert into account_log");
        sqlString.add("select ?, account_id, money_amount - ?, money_amount");
        sqlString.add("    , ?, now(), ?");
        sqlString.add("from account_info");
        sqlString.add("where account_id = ?");
        return jdbcSupportTemplate.execute(sqlString.build(),
                new Object[]{
                        accountLog.getLogId(),
                        accountLog.getBalanceAmount(),
                        accountLog.getBalanceAmount().abs(),
                        accountLog.getChangeCause(),
                        accountLog.getAccountId()
                });
    }

    @Override
    public AccountLogDo selectAccountLogByAccountId(String accountId) {
        SqlString sqlString=SqlString.newInstance();
        sqlString.add(" select * from ACCOUNT_LOG where ACCOUNT_ID = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{accountId},AccountLogDo.class);
    }

    @Override
    public void update(AccountLogDo accountLogDo) {
        jdbcSupportTemplate.updateSelective(accountLogDo);
    }

    @Override
    public void save(AccountLogDo accountLogDo) {
        jdbcSupportTemplate.insert(accountLogDo);
    }


}
